#ifndef _DEFINE_
#define _DEFINE_

class Folder;
class Message
{
public:
	//构造函数
	Message(const std::string &str = ""):contents(str){}
	//复制构造函数
	Message(const Message& m):contents(m.contents),folders(m.folders)
	{
		put_Msg_in_Folders(folders);
	}
	//赋值操作符
	Message& operator=(const Message& rhs)
	{
		if(&rhs != this)
		{
			this->remove_Msg_from_Folders();
			contents = rhs.contents;
			folders = rhs.folders;
			put_Msg_in_Folders(rhs.folders);
		}
		return *this;
	}
	//析构函数
	~Message()
	{
		remove_Msg_from_Folders();
	}
	/*
	*在Message类中增加save和remove操作。这些操作应接受一个Folder,并将该Folder加入到指向这个Message的Folder集中（或从其中删除Folder）。
	*操作还必须更新Folder以及反映它指向该Message,这可以通过调用addMsg或remMsg完成
	*/
	void save(Folder& fld)
	{
		addFLdr(&fld);
		fld.addMsg(this);
	}
	void remove(Folder& fld)
	{
		remFLdr(&fld);
		fld.remMsg(this);
	}
private:
	std::string contents;
	std::set<Folder*> folders;

	void put_Msg_in_Folders(const std::set<Folder*> &rhs)
	{
		for(std::set<Folder*>::const_iterator beg = rhs.begin();beg != rhs.end();++beg)
		{
			(*beg)->addMsg(this);
		}
	}

	void remove_Msg_from_Folders()
	{
		for(std::set<Folder*>::const_iterator beg = folders.begin();beg != folders.end();++beg)
		{
			(*beg)->remMsg(this);
		}
	}

	void addFLdr(Folder *pFldr)
	{
		folders.insert(pFldr);
	}

	void remFLdr(Folder *pFldr)
	{
		folders.erase(pFldr);
	}

	
};

class Folder
{
public:
	void addMsg(Message *m);
	void remMsg(Message *m);
};

#endif



